package com.bobo.service.impl;

import com.bobo.base.mybatis.ServiceImpl;
import com.bobo.mapper.BasketMapper;
import com.bobo.mapper.CategoryMapper;
import com.bobo.pojo.Basket;
import com.bobo.pojo.Category;
import com.bobo.pojo.dto.ShopCartItemDto;
import com.bobo.service.BasketService;
import com.bobo.service.CategoryService;
import com.bobo.util.Arith;
import com.bobo.util.CacheManagerUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BasketServiceImpl  extends ServiceImpl<BasketMapper, Basket> implements BasketService {
    @Resource
    private BasketMapper basketMapper;
    @Resource
    private CacheManagerUtil cacheManagerUtil;
    @Override
    public List<ShopCartItemDto> getShopCartItems(String userId) {
        // 在这个类里面要调用这里的缓存信息，并没有使用aop，所以不使用注解
        List<ShopCartItemDto> shopCartItemDtoList = cacheManagerUtil.getCache("ShopCartItems", userId);
        if (shopCartItemDtoList != null) {
            return shopCartItemDtoList;
        }
        shopCartItemDtoList = basketMapper.getShopCartItems(userId);
        for (ShopCartItemDto shopCartItemDto : shopCartItemDtoList) {
            shopCartItemDto.setProductTotalAmount(Arith.mul(shopCartItemDto.getProdCount(), shopCartItemDto.getPrice()));
        }
        cacheManagerUtil.putCache("ShopCartItems", userId, shopCartItemDtoList);
        return shopCartItemDtoList;
    }
}
